package com.nl.controller.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.JzzwCheck;
import com.nl.controller.web.mapper.JzzwCheckMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class JzzwCheckService {
	@Autowired
	private JzzwCheckMapper jzzwCheckMapper;
	@Autowired
	private CommonService commonService;
	
	@SystemServiceLog(description = "查询精准政务审核分页信息", value = false)
	public PageInfo<JzzwCheck> findJzzwCheck(int page, int limit, String cityId, String mgrId, String mgrName,String checkResult) {
		Map<String, Object> map = new HashMap<>();
		map.put("cityId", cityId);
		map.put("mgrId", mgrId);
		map.put("mgrName", mgrName);
		map.put("checkResult", checkResult);
		List<JzzwCheck> list= new ArrayList<>();

		try {
			PageHelper.startPage(page, limit);
			list = jzzwCheckMapper.findJzzwCheck(map);
            for (JzzwCheck jzzwCheck : list) {
				if ("1".equals(jzzwCheck.getTaskStatus())){
				jzzwCheck.setTaskStatusName("开启");
				}else if ("2".equals(jzzwCheck.getTaskStatus())){
				jzzwCheck.setTaskStatusName("关闭");
				}else if ("3".equals(jzzwCheck.getTaskStatus())){
				jzzwCheck.setTaskStatusName("开启");
				}
				
				if("1".equals(jzzwCheck.getCheckResult())){
				jzzwCheck.setCheckResultName("审核通过");
				}else if ("2".equals(jzzwCheck.getCheckResult())){
				jzzwCheck.setCheckResultName("审核不通过");
				}
            }
			commonService.createSmsOperLog("4", "查询精准政务审核", "0", "精准政务");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询模板信息表");
			commonService.createSmsOperLog("4", "查询精准政务审核", "1", "精准政务");
			throw new SmsException("查询精准政务审核出现异常");
		}

		PageInfo<JzzwCheck> pageInfo = new PageInfo<JzzwCheck>(list);
		return pageInfo;
	}
	
	@SystemServiceLog(description = "根据地市的id查询精准政务信息", value = false)
	public List<JzzwCheck> findMgrByCityId(String cityId) {
		List<JzzwCheck> list = new ArrayList<JzzwCheck>();
		try {
			list = jzzwCheckMapper.findMgrByCityId(cityId);
			log.info("根据地市的id查询项目信息：" + cityId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("根据地市的id查询项目信息：" + cityId);
			throw new SmsException("根据地市的id查询项目信息：" + cityId);
		}
		return list;
	}
	
	@SystemServiceLog(description = "精准政务项目名称联想", value = false)
	public List<JzzwCheck> selectMgrNameLike(String key, String cityId) {
 		List<JzzwCheck> list = jzzwCheckMapper.selectMgrNameLike(key, cityId);
		return list;
	}
	
	@SystemServiceLog(description = "通过项目id查询项目模板表", value = false)
    public JzzwCheck findJzzwCheckByMgrId(String mgrId) {
    	JzzwCheck jzzwCheck;
        try {
        	jzzwCheck = jzzwCheckMapper.findJzzwCheckByMgrId(mgrId);
            commonService.createSmsOperLog("4", "查询精准政务模板表根据项目id"+ mgrId, "0", "精准政务");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询项目模板表根据项目id： " + mgrId);
            commonService.createSmsOperLog("4", "查询精准政务模板表根据项目id"+ mgrId, "1", "精准政务");
            throw new SmsException("查询项目模板表根据项目id： " + mgrId);
        }
        return jzzwCheck;
    }
    
    @SystemServiceLog(description = "精准政务模板审核", value = false)
    public int templateCheck(String mgrId, String checkResult, String checkMsg) {
        int result = 0;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("checkMsg", checkMsg);
        map.put("mgrId", mgrId);
        map.put("checkResult", checkResult);
        try {
        	result = jzzwCheckMapper.templateCheck(map);
            commonService.createSmsOperLog("3", "修改审核信息"+mgrId, "0", "精准政务审核");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("3", "修改审核信息"+mgrId, "1", "精准政务审核");
            log.error("模板审核出现异常" + mgrId);
            throw new SmsException("模板审核出现异常" + mgrId);
        }
        return result;
    }
}
